use "actions T1 - T6.dta", clear

keep if treatment==4|treatment==3

expand 91
sort session Subject Period
by session Subject Period: gen TimeStamp=_n
replace TimeStamp=TimeStamp-1

sort session Subject Period TimeStamp

drop if TimeStamp>50&(session == "150520_1335"|session == "150520_0847"|session=="150519_1349")

keep session Period Subject OwnAction TimeStamp treatment
sort session Period Subject TimeStamp

save pippo, replace


use "messages bare communication.dta", clear


***we drop observations were more message C were sent in the same value of TimaStamp

sort session Period Subject TimeStamp seq

by session Period Subject TimeStamp: keep if _n==_N
keep session Period Subject TimeStamp Message
joinby session Period Subject TimeStamp using pippo, unmatched(both)


gen message=0
replace message=1 if Message=="C"
replace message=2 if Message=="B"|Message=="A"

sort session Subject Period TimeStamp
gen contollo=message

replace message=. if message==0
by session Subject Period :replace message=message[_n-1] if TimeStamp>1&message==.
by session Subject Period : egen indicatore=mean(message) 

sort session Subject Period TimeStamp
by session Subject Period: keep if _n==_N



gen indicat=indicatore==1
tab indicat OwnAction if message==1

***  All
gen OwnC=OwnAction==3
tab indicat OwnC if message==1, row chi2

*** Clust and NoClust

sort treatment
by treatment: tab indicat OwnC if message==1, row chi2

